/**
 * Cross-browser marquee.
 *
 * @author Andrey Anisimov <hercules@inbox.ru>
 */

import core.ui.CompositeWindow;
import core.util.Timer;

class Marquee extends CompositeWindow {
	
	/**
	 * Creates new marquee.
	 * 
	 * @param parent (Window) [optional] parent window
	 * @param x, y (int) [optional] coordinates
	 * @param width, height (int) [optional] marquee size.
	 */
	this.Marquee = function(parent, x, y, width, height) {
		super(parent, x, y, width, height);
		this.loop = 0;			// The number of times a marquee will play
		this.scrollAmount = 6;	// The scroll amount in pixels the text scrolls each subsequent
		this.currentLoop = 0;
		this.timer = new Timer(85);
		this.timer.addTimerListener(this);
		this.updateLayout();
		this.start();
	}
	
	/**
	 * Sets the text for marquee.
	 * 
	 * @param text (string) text to scroll
	 */
	this.setText = function(text) {
		this.text.innerHTML = text;
	}
	
	/**
	 * Returns current marquee text.
	 *
	 * @return string
	 */
	this.getText = function() {
		return this.text.innerHTML;
	}
	
	/**
	 * Sets the number of times a marquee will play.
	 * If 0 or -1, marquee wil loops infinitely.
	 *
	 * @param loop (int) loop count
	 */
	this.setLoop = function(loop) {
		this.loop = loop;
	}
	
	/**
	 * Returns current loop count.
	 *
	 * @return int
	 */
	this.getLoop = function() {
		return this.loop;
	}
	
	/**
	 * Sets the number of pixels the text scrolls
	 * between each subsequent drawing of the marquee.
	 *
	 * @param amount (int) scroll amount
	 */
	this.setScrollAmount = function(amount) {
		this.scrollAmount = amount;
	}
	
	/**
	 * Returns the number of pixels the text scrolls.
	 *
	 * @return int
	 */
	this.getScrollAmount = function() {
		return this.scrollAmount;
	}
	
	/**
	 * Sets the speed of the marquee scroll.
	 * 
	 * @param delay (int) delay in milliseconds
	 */
	this.setScrollDelay = function(delay) {
		this.timer.setInterval(delay);
	}
	
	/**
	 * Retrieves the speed of the marquee scroll.
	 * 
	 * @return int
	 */
	this.getScrollDelay = function() {
		return this.timer.getInterval();
	}
	
	/**
	 * Starts scrolling the marquee.
	 */
	this.start = function() {
		this.stop();
		this.currentLoop = 0;
		this.text.style.left = this.getWidth() + 'px';
		this.timer.start();
	}
	
	/**
	 * Stops the marquee from scrolling.
	 */
	this.stop = function() {
		this.timer.stop();
	}
	
	/**
	 * Gets whether the marquee scrolling is started.
	 *
	 * @return bool
	 */
	this.isStarted = function() {
		return this.timer.isStarted();
	}
	
	/**
	 * Stops and closes marquee.
	 */
	this.close = function() {
		if (this.isStarted()) this.stop();
		super.close();
	}
	
	/**
	 * @protected
	 * Calls automatically every time marquee size is changed.
	 */
	this.updateLayout = function() {
		this.text.style.height = this.text.style.lineHeight = this.getHeight() + 'px';
	}
	
	/**
	 * @protected
	 * Creates and returns root element for list box.
	 *
	 * @return HTMLElement
	 */
	this.createElement = function() {
		var root = super.createElement();
		this.text = document.createElement('div');
		this.text.style.position = 'absolute';
		this.text.style.whiteSpace = 'nowrap';
		root.appendChild(this.text);
		return root;
	}

	this.onTimer = function() {
		this.text.style.left = (this.text.offsetLeft - this.scrollAmount) + 'px';
		// Check for new loop
		if (this.text.offsetLeft + this.text.offsetWidth <= 0) {
			// Check loop counter
			this.currentLoop++;
			if ((this.loop > 0) && (this.currentLoop >= this.loop)) {
				this.stop();
				return;
			}
			// Start new loop
			this.text.style.left = this.getWidth() + 'px';
		}
		this.timer.start();
	}
}